package edu.spangler.dang.FriendViewer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

/**
 * Class that holds the MapActivity where Google Maps is used
 * to display all the available friends' current locations as
 * icons on the map.
 * @author Tuan Dang
 */
public class FriendMap extends MapActivity {

	private MapView mapView;
	private List<Overlay> overlays;
	private Drawable draw;
	private ItemizedOverlay itemOverlay;
	private ArrayList<FriendObject> friends;
	private ArrayList<Object> objs;
	
	/**
	 * Creates the a GUI screen
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		// Extract information from intents
		Intent prevIntent = this.getIntent();
		Serializable s = prevIntent.getSerializableExtra("friends");
		Object[] objs = (Object[]) s;
		if (objs != null) {
			friends = new ArrayList<FriendObject>();
			for (Object o : objs) {
				if (o instanceof FriendObject)
					friends.add((FriendObject) o);
			}
		}
		String centerName = prevIntent.getStringExtra("username");
		double centerLatitude = prevIntent.getDoubleExtra("centerLatitude", 0);
		double centerLongitude = prevIntent.getDoubleExtra("centerLongitude", 0);
		
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.getController().setCenter(new GeoPoint((int)(centerLatitude * 1E6), (int)(centerLongitude * 1E6)));
		
		overlays = mapView.getOverlays();
		draw = this.getResources().getDrawable(R.drawable.icon);
		itemOverlay = new ItemizedOverlay(draw, this, centerName);
		overlays.add(itemOverlay);
		
		// adds icons for each friend on the list
		for(FriendObject friend : friends) {
			itemOverlay.addOverlay(new OverlayItem(new GeoPoint((int)(friend.getLatitude() * 1E6),
					(int)(friend.getLongitude() * 1E6)), friend.getUsername(), "Friend Marker"));
		}
	}
	
	/**
	 * Method that allows for the display of routes.
	 */
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	/**
	 * Method that reads data from the content provider and
	 * then draws icons for each friend on a refreshed map.
	 */
	@SuppressWarnings("unchecked")
	public void processAndDisplay() {		
		objs = ProcessContentProvider.process(this, false);
		
		if ((Integer)(objs.get(1)) > 0) {	
			friends = (ArrayList<FriendObject>)objs.get(2);
			// force a redraw of the map
			mapView.postInvalidate();
			overlays = mapView.getOverlays();
			overlays.clear();
			draw = this.getResources().getDrawable(R.drawable.icon);
			itemOverlay = new ItemizedOverlay(draw, this, "Center");
			overlays.add(itemOverlay);
			
			// add icons for each friend in the list
			for(FriendObject friend : friends) {
				itemOverlay.addOverlay(new OverlayItem(new GeoPoint((int)(friend.getLatitude() * 1E6),
						(int)(friend.getLongitude() * 1E6)), friend.getUsername(), "Friend Marker"));
			}
		}
	}
	
	/**
	 * A Broadcast Receiver that listens for a content provider changed
	 * action. Will update the GUI to reflect said changes.
	 */
	private BroadcastReceiver friendReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			processAndDisplay();
		}
	};
	
	/**
	 * Method where when the FriendViewer activity is resumed, will
	 * set up the Broadcast Receiver and refresh the GUI for any changes.
	 */
	public void onResume() {
		super.onResume();
		
		IntentFilter filter = new IntentFilter();
		filter.addAction("edu.spangler.dang.FriendsUpdated");
		
		this.registerReceiver(this.friendReceiver, filter);
		processAndDisplay();
	}
	
	/**
	 * Method that will turn off the Broadcast Receiver when FriendViewer
	 * activity is paused.
	 */
	public void onPause() {
		super.onPause();
		
		this.unregisterReceiver(this.friendReceiver);
	}
}
